package com.legsem.jrhosync.samples;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;

import com.legsem.jhosync.Context;
import com.legsem.jhosync.face.JhosyncSource;
import com.legsem.jhosync.face.SourceAdapter;
import com.legsem.jhosync.face.SourceAdapterException;

/**
 * Source implementations must extend the SourceAdapter class and have a
 * JhosyncSource annotation that holds the metadata.
 * <p/>
 * This sample also produces metadata.
 * 
 */
@JhosyncSource(name = "Appointment")
public class AppointmentSource extends SourceAdapter {

    /** A JSON mapper for JSON handling. */
    private ObjectMapper mapper = new ObjectMapper();

    public void login(Context context) throws SourceAdapterException {

    }

    public Map < String, Map < String, Object > > query(
            Map < String, Object > params) throws SourceAdapterException {
        return formatResults("[{\"appointment\":{"
                + "\"created_at\":\"2011-04-12T15:08:57Z\","
                + "\"notes\":\"Préparation contrat\","
                + "\"updated_at\":\"2011-04-12T15:08:57Z\","
                + "\"date\":\"2011-04-12T15:07:00Z\"," + "\"id\":2,"
                + "\"customer_id\":2," + "\"salesrep_id\":2}}]");

    }

    /**
     * Reformats the backend reply so that records are associated with their
     * primary key.
     * 
     * @param json the backend response
     * @return a Map conforming to the sync server expectations
     * @throws SourceAdapterException if response cannot be reformatted
     */
    @SuppressWarnings("unchecked")
    protected Map < String, Map < String, Object > > formatResults(String json)
            throws SourceAdapterException {
        try {
            Map < String, Map < String, Object > > results = new HashMap < String, Map < String, Object > >();
            Map < String, Map < String, Object > >[] array = mapper.readValue(
                    json, Map[].class);
            for (Map < String, Map < String, Object > > entry : array) {
                Map < String, Object > customer = entry.get("appointment");
                results.put(customer.get("id").toString(), customer);
            }
            return results;
        } catch (IOException e) {
            throw new SourceAdapterException(e);
        }
    }

    public String create(Map < String, Object > params)
            throws SourceAdapterException {
        return Integer.toString(0);
    }

    public void update(Map < String, Object > attributes)
            throws SourceAdapterException {
        // TODO Auto-generated method stub

    }

    public void delete(Map < String, Object > attributes)
            throws SourceAdapterException {
        // TODO Auto-generated method stub

    }

    public void logoff() throws SourceAdapterException {

    }

    @Override
    public String metadata() {
        return "{\"indexdata\":" + "  {\"title\": \"UI meta panel\","
                + "   \"type\" : \"iuipanel\"," + "   \"children\" :" + "   ["
                + "    {\"label\": \"Table\"," + "      \"type\": \"table\","
                + "      \"children\":" + "        [ " + "          {"
                + "            \"label\": \"Customer id:\","
                + "            \"value\": \"{{customer_id}}\","
                + "            \"name\": \"customer_id\","
                + "            \"type\": \"labeledrow\" " + "          },"
                + "          {" + "            \"label\": \"Notes:\","
                + "            \"value\": \"{{notes}}\","
                + "            \"name\": \"notes\","
                + "            \"type\": \"labeledrow\" " + "          }"
                + "        ]," + "      \"repeatable\": \"{{@appointments}}\" "
                + "    }" + "   ]" + "  }" + "}";

    }
}
